package com.ecar.apm.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.ecar.apm.model.HttpSequenceLog;
import com.ecar.apm.model.RecentStatus;

@Mapper
public interface HttpSequenceLogMapper {

	  @Insert({"INSERT INTO http_sequence_log(pguid,`status`,`costTime`,log)  VALUES(#{httpSequenceLog.pguid},#{httpSequenceLog.status},#{httpSequenceLog.costTime},#{httpSequenceLog.log})"})
	  @Options(useGeneratedKeys=true, keyProperty="httpSequenceLog.id", keyColumn="id")
	  public abstract void insert(@Param("httpSequenceLog") HttpSequenceLog paramHttpSequenceLog);
	  
	  @Select({"SELECT * FROM http_sequence_log"})
	  public abstract List<HttpSequenceLog> selectAll();
	  
	  @Select({"select t.* from http_sequence_log t where t.pguid=#{pguid} and t.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) order by createTime desc"})
	  public abstract HttpSequenceLog getByGuid(@Param("pguid") String paramString);
	  
	  @Delete({"delete from http_sequence_log where pguid=#{pguid}"})
	  public abstract void delete(@Param("pguid") String paramString);
	  
	  @Select({"select a.costTime,a.moniCount,a.minCost,a.maxCost,b.lastCost,b.createTime,b.status from ( select pguid,ROUND(AVG(t.costTime)) as costTime,count(1) as moniCount,min(t.costTime) minCost,max(t.costTime) maxCost from http_sequence_log t where t.pguid = #{pguid} and t.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) ) a left join (select p.pguid,p.costTime lastCost,timediff(now(),p.createTime) createTime,p.`status` from http_sequence_log p where p.pguid = #{pguid} and p.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) order by p.createTime desc limit 1 ) b on a.pguid=b.pguid"})
	  public abstract RecentStatus selectRecentStatusByPguid(@Param("pguid") String paramString);
	  
	  @Select({"select t.`status`,count(t.`status`) as count from http_sequence_log t where t.pguid = #{pguid} and t.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) group by t.`status`"})
	  public abstract List<Map<String, Object>> selectUsabilityByPguid(@Param("pguid") String paramString);
	  
	  @Select({"select ROUND(AVG(t.costTime)) as costTime from http_sequence_log t where t.pguid = #{pguid} and t.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) order by t.createTime"})
	  public abstract Object selectAvgCostTimeByPguid(@Param("pguid") String paramString);
	  
	  @Select({"select t.id,t.pguid,t.createTime,t.`status`,t.costTime,t.log from http_sequence_log t where t.pguid = #{pguid} and t.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) order by t.createTime desc"})
	  public abstract List<Map<String, Object>> selectLogByPguid(@Param("pguid") String paramString);
	  
	  @Select({"select DATE_FORMAT(t.createTime,'%H:%i:%s') as name,t.costTime as value from http_sequence_log t where t.pguid = #{pguid} and t.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) order by t.createTime"})
	  public abstract List<Map<String, Object>> selectLogByPguidForEcharts(@Param("pguid") String paramString);
	  
	  @Select({"select costTime as name,count(1) as value from ( select case when t.costTime<=10 then 10 when t.costTime>10 and t.costTime<=50 then 20 when t.costTime>50 and t.costTime<=100 then 30 when t.costTime>100 then 40 end costTime from http_sequence_log t  where t.pguid = #{pguid} and t.createTime>DATE_SUB(now(),INTERVAL 24 HOUR) ) a group by costTime order by 1"})
	  public abstract List<Map<String, Object>> selectLogByPguidForEchartsPie(@Param("pguid") String paramString);
	  
	  @Delete({"delete from http_sequence_log where createTime < date_sub(curdate(),interval #{day} day)"})
	  public abstract void cleanLogByDay(@Param("day") int paramInt);
}
